package com.xrath.rtm.test;

import java.io.Console;
import java.net.URL;
import java.util.*;
import java.util.logging.*;
import com.xrath.rtm.*;

/**
 *
 */
public class Main
{
	public static void main( String[] args ) throws Exception
	{
		Logger log = Logger.getLogger("com.xrath.rtm.RememberTheMilk");
		log.setLevel( Level.FINER );

		Console c = System.console();

		RememberTheMilk rtm = new RememberTheMilk();
		rtm.setAPIKey( System.getProperty("api.key") );
		rtm.setSharedSecret( System.getProperty("shared.secret") );

		if( args[0].equals("token") ) {
			String frob = rtm.getFrob();
			URL url = rtm.getAuthenticationURL("delete", frob);

			c.printf("Confirm the following URL: %s%n", url);
			c.printf("Done? ");
			new Scanner(System.in).nextLine();

			rtm.createToken(frob);
		} else {
			rtm.setToken( args[0] );
		}

		TaskList taskList = null;

		List<TaskList> lists = rtm.getTaskLists();
		for(TaskList list : lists) {
			c.printf("%s (%d)%n", list.getName(), list.getId());
			if( list.getName().equals("프로그래밍") )
				taskList = list;
		}

		List<Task> tasks = rtm.getTasks(taskList, "status:incomplete");
		for(Task task : tasks)
		{
			c.printf("%-50s - added=%s, priority=%d, tags=%s%n", 
				task.getName(), task.getAdded(), task.getPriority(), task.getTags());
		}


		rtm.createTimeline();

		Task toAdd = new Task("Check the milk and buy it");
		toAdd.setURL( new URL("http://www.rememberthemilk.com/") );
		toAdd.setPriority(2);
		toAdd.setEstimate("30 min");
		toAdd.addTag("moment");
		toAdd.addNote(new Note("Note title", "note content"));

		Calendar cal = Calendar.getInstance();
		cal.add( Calendar.DAY_OF_MONTH, 3 );

		toAdd.setDue( cal.getTime() );

		rtm.addTask(taskList, toAdd);
	}
}
